﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using CNVP_SilverlightClass.getInfo;

namespace CNVP_SilverlightClass
{
    public class Drag 
    {
        //矩形对象的右上角
        public string curentName { get; set; }
        string oldName;

        bool isMouseCaptured = false;
        double mouseVerticalPosition;
        double mouseHorizontalPosition;
        public Canvas MainMap { get; set; }
        public string TextBlockName { get; set; }
        //删除
        bool isPoly = false;

        //封装的矩形创建方法
        public void CreateRecForCan(bool canCreate, bool inCreation, string cuRectName, double x, double y, double w, double h, string tooltip)
        {
            Rectangle rec;

            //由于鼠标可能向上或向下拖动，因此，为了取得X和Y坐标，须要选取比较小的坐标值，以便于重新指定矩形在画布中的位置。
            double X = x;
            double Y = y;

            //不断的指定矩形的宽和高
            double W = w;
            double H = h;

            //创建新矩形。
            rec = new Rectangle();
            rec.SetValue(Canvas.LeftProperty, X);
            rec.SetValue(Canvas.TopProperty, Y);
            rec.Width = W;
            rec.Height = H;
            rec.Name = cuRectName;
            rec.StrokeThickness = 1;
            ToolTipService.SetToolTip(rec, tooltip);
            rec.Stroke = new SolidColorBrush(Colors.Blue);
            // 蓝色, 1/2透明 
            rec.Fill = new SolidColorBrush(Color.FromArgb(50, 49, 112, 165));
            rec.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseLeftButtonDown);
            
            //添加拖拽事件
            rec.MouseMove += new MouseEventHandler(Handle_MouseMove);
            rec.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseLeftButtonUp);
            rec.MouseRightButtonDown += new MouseButtonEventHandler(Handle_MouseRightButtonDown);
            //叠加个文本
            TextBlock tb = new TextBlock();
            tb.Text = TextBlockName;
            tb.Name = rec.Name + "abc";
            tb.SetValue(Canvas.LeftProperty, X + 5);
            tb.SetValue(Canvas.TopProperty, Y + 5);
            //将矩形添加到画布
            MainMap.Children.Add(rec);
            MainMap.Children.Add(tb);

        }

        //封装的矩形创建方法,无法拖动
        public void NoMove_CreateRecForCan(bool canCreate, bool inCreation, string cuRectName, double x, double y, double w, double h, string tooltip)
        {
            Rectangle rec;

            //由于鼠标可能向上或向下拖动，因此，为了取得X和Y坐标，须要选取比较小的坐标值，以便于重新指定矩形在画布中的位置。
            double X = x;
            double Y = y;

            //不断的指定矩形的宽和高
            double W = w;
            double H = h;

            //创建新矩形。
            rec = new Rectangle();
            rec.SetValue(Canvas.LeftProperty, X);
            rec.SetValue(Canvas.TopProperty, Y);
            rec.Width = W;
            rec.Height = H;
            rec.Name = cuRectName;
            ToolTipService.SetToolTip(rec, tooltip);
            //叠加个文本
            TextBlock tb = new TextBlock();
            tb.Text = TextBlockName;
            tb.Name = rec.Name + "abc";
            tb.SetValue(Canvas.LeftProperty, X + 5);
            tb.SetValue(Canvas.TopProperty, Y + 5);
            //将矩形添加到画布
            MainMap.Children.Add(rec);
            MainMap.Children.Add(tb);

        }

        public void Handle_MouseMove(object sender, MouseEventArgs args)
        {
            if (!isMouseCaptured) return;
            Rectangle item = sender as Rectangle;
            if (isMouseCaptured)
            {
                //获取当前对象坐标的位移
                double deltaV = args.GetPosition(null).Y - mouseVerticalPosition;
                double deltaH = args.GetPosition(null).X - mouseHorizontalPosition;
                //获取当前对象起始坐标
                double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
                double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);
                // 设置矩形的新起始位置
                item.SetValue(Canvas.TopProperty, newTop);
                item.SetValue(Canvas.LeftProperty, newLeft);
                //跟着一起移动
                TextBlock tb = (TextBlock)MainMap.FindName(item.Name + "abc");
                tb.SetValue(Canvas.TopProperty, newTop + 5);
                tb.SetValue(Canvas.LeftProperty, newLeft + 5);
                // 更新全局坐标
                mouseVerticalPosition = args.GetPosition(null).Y;
                mouseHorizontalPosition = args.GetPosition(null).X;

                ResetThumb(item);
            }
        }

        public void Handle_MouseLeftButtonDown(object sender, MouseButtonEventArgs args)
        {
            isPoly = false;
            isMouseCaptured = true;//处理标志位
            Rectangle item = sender as Rectangle;
            mouseVerticalPosition = args.GetPosition(null).Y;
            mouseHorizontalPosition = args.GetPosition(null).X;
            oldName = curentName;
            curentName = item.Name;
            AddThumbForCan("thumb0", MainMap, item, ((double)item.GetValue(Canvas.LeftProperty) - 3), ((double)item.GetValue(Canvas.TopProperty) - 3));
            AddThumbForCan("thumb1", MainMap, item, ((double)item.GetValue(Canvas.LeftProperty) + (double)item.Width - 3), ((double)item.GetValue(Canvas.TopProperty) - 3));
            AddThumbForCan("thumb2", MainMap, item, ((double)item.GetValue(Canvas.LeftProperty) - 3), ((double)item.GetValue(Canvas.TopProperty) + (double)item.Height - 3));
            AddThumbForCan("thumb3", MainMap, item, ((double)item.GetValue(Canvas.LeftProperty) + (double)item.Width - 3), ((double)item.GetValue(Canvas.TopProperty) + (double)item.Height - 3));
            item.CaptureMouse();
        }

        public void Handle_MouseLeftButtonUp(object sender, MouseButtonEventArgs args)
        {
            Rectangle item = sender as Rectangle;
            isMouseCaptured = false;//停止拖动
            item.ReleaseMouseCapture();//释放鼠标

            mouseVerticalPosition = -1;
            mouseHorizontalPosition = -1;
            ResetThumb(item);
        }

        public void Handle_MouseRightButtonDown(object sender, MouseButtonEventArgs args)
        {
            Rectangle item = sender as Rectangle;
            isMouseCaptured = false;
            item.ReleaseMouseCapture();
            mouseVerticalPosition = -1;
            mouseHorizontalPosition = -1;

            ResetThumb(item);
            args.Handled = true;
        }

        public void pl_MouseLeftButtonDown(object sender, MouseEventArgs args)
        {
            isPoly = true;
            isMouseCaptured = true;
            
        }
       
        public void pl_MouseRightButtonDown(object sender, MouseButtonEventArgs args)
        {
            oldName = curentName;
            isPoly = true;
            isMouseCaptured = true;
        }
        //Thumb控件拖拽事件，调整Thumb控件的位置，同时改变对应矩形的外观
        public void thumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            System.Windows.UIElement ui = (System.Windows.UIElement)sender;
            double x = Canvas.GetLeft(ui) + e.HorizontalChange; //移动的水平距离
            double y = Canvas.GetTop(ui) + e.VerticalChange; //移动的坚直距离
            Canvas.SetLeft(ui, x);
            Canvas.SetTop(ui, y);
            Thumb sendFrom = (Thumb)sender;
            Rectangle rec = (Rectangle)(MainMap.FindName(curentName));
            TextBlock tb = (TextBlock)MainMap.FindName(rec.Name + "abc");
            switch (sendFrom.Name.ToString())
            {
                case "thumb0":
                    rec.SetValue(Canvas.TopProperty, ((double)rec.GetValue(Canvas.TopProperty) + e.VerticalChange));
                    rec.SetValue(Canvas.LeftProperty, ((double)rec.GetValue(Canvas.LeftProperty) + e.HorizontalChange));
                    if ((rec.Width - e.HorizontalChange) > 10 && (rec.Height - e.VerticalChange) > 10)
                    {
                        rec.Width = rec.Width - e.HorizontalChange;
                        rec.Height = rec.Height - e.VerticalChange;
                    }
                    //TextBlock
                    tb.SetValue(Canvas.TopProperty, ((double)tb.GetValue(Canvas.TopProperty) + e.VerticalChange));
                    tb.SetValue(Canvas.LeftProperty, ((double)tb.GetValue(Canvas.LeftProperty) + e.HorizontalChange));
                    if ((tb.Width - e.HorizontalChange) > 10 && (tb.Height - e.VerticalChange) > 10)
                    {
                        tb.Width = rec.Width - e.HorizontalChange;
                        tb.Height = rec.Height - e.VerticalChange;
                    }
                    break;
                case "thumb1":
                    rec.SetValue(Canvas.TopProperty, ((double)rec.GetValue(Canvas.TopProperty) + e.VerticalChange));
                    if ((rec.Width + e.HorizontalChange) > 10 && (rec.Height - e.VerticalChange) > 10)
                    {
                        rec.Width = rec.Width + e.HorizontalChange;
                        rec.Height = rec.Height - e.VerticalChange;
                    }
                    //TextBlock
                    tb.SetValue(Canvas.TopProperty, ((double)tb.GetValue(Canvas.TopProperty) + e.VerticalChange));
                    if ((rec.Width + e.HorizontalChange) > 10 && (tb.Height - e.VerticalChange) > 10)
                    {
                        tb.Width = tb.Width + e.HorizontalChange;
                        tb.Height = tb.Height - e.VerticalChange;
                    }
                    break;
                case "thumb2":
                    rec.SetValue(Canvas.LeftProperty, ((double)rec.GetValue(Canvas.LeftProperty) + e.HorizontalChange));
                    if ((rec.Width - e.HorizontalChange) > 10 && (rec.Height + e.VerticalChange) > 10)
                    {
                        rec.Width = rec.Width - e.HorizontalChange;
                        rec.Height = rec.Height + e.VerticalChange;
                    }
                    //TextBlock
                    tb.SetValue(Canvas.LeftProperty, ((double)tb.GetValue(Canvas.LeftProperty) + e.HorizontalChange));
                    if ((tb.Width - e.HorizontalChange) > 10 && (tb.Height + e.VerticalChange) > 10)
                    {
                        tb.Width = tb.Width - e.HorizontalChange;
                        tb.Height = tb.Height + e.VerticalChange;
                    }
                    break;
                case "thumb3":
                    if ((rec.Width + e.HorizontalChange) > 10 && (rec.Height + e.VerticalChange) > 10)
                    {
                        rec.Width = rec.Width + e.HorizontalChange;
                        rec.Height = rec.Height + e.VerticalChange;
                    }
                    //TextBlock
                    if ((tb.Width + e.HorizontalChange) > 10 && (tb.Height + e.VerticalChange) > 10)
                    {
                        tb.Width = tb.Width + e.HorizontalChange;
                        tb.Height = tb.Height + e.VerticalChange;
                    }
                    break;
                default:
                    break;
            }
            ResetThumb(rec);
        }
        //设置4个Thumb的位置
        public void ResetThumb(Rectangle item)
        {
            Canvas.SetLeft((Thumb)(MainMap.FindName("thumb0")), ((double)item.GetValue(Canvas.LeftProperty) - 3));
            Canvas.SetTop((Thumb)(MainMap.FindName("thumb0")), ((double)item.GetValue(Canvas.TopProperty) - 3));

            Canvas.SetLeft((Thumb)(MainMap.FindName("thumb1")), ((double)item.GetValue(Canvas.LeftProperty) + (double)item.Width - 3));
            Canvas.SetTop((Thumb)(MainMap.FindName("thumb1")), ((double)item.GetValue(Canvas.TopProperty) - 3));

            Canvas.SetLeft((Thumb)(MainMap.FindName("thumb2")), ((double)item.GetValue(Canvas.LeftProperty) - 3));
            Canvas.SetTop((Thumb)(MainMap.FindName("thumb2")), ((double)item.GetValue(Canvas.TopProperty) + (double)item.Height - 3));

            Canvas.SetLeft((Thumb)(MainMap.FindName("thumb3")), ((double)item.GetValue(Canvas.LeftProperty) + (double)item.Width - 3));
            Canvas.SetTop((Thumb)(MainMap.FindName("thumb3")), ((double)item.GetValue(Canvas.TopProperty) + (double)item.Height - 3));
        }
        //Thumb控件的鼠标进入事件，用来改变鼠标指针样式未手型。
        public void thumb_MouseEnter(object sender, MouseEventArgs e)
        {
            Thumb sendFrom = (Thumb)sender;//获取事件对象，转换为Thumb
            sendFrom.Cursor = Cursors.Hand;//转换指针未手型
        }
        //封装添加Thumb控件的方法
        public void AddThumbForCan(string thumbName, Canvas CanItem, Rectangle item, double left, double top)
        {
            Thumb thumbB;
            //删除已经存在的同名的Thumb控件
            if (CanItem.FindName(thumbName) != null) CanItem.Children.Remove((Thumb)(CanItem.FindName(thumbName)));
            thumbB = new Thumb() { Width = 6, Height = 6 };//初始化控件大小
            Canvas.SetZIndex(thumbB, 1005);//初始化层叠位置
            thumbB.Name = thumbName;//初始化控件Name
            Canvas.SetLeft(thumbB, left);//初始化控件X坐标
            Canvas.SetTop(thumbB, top);//初始化控件Y坐标

            thumbB.DragDelta += new DragDeltaEventHandler(thumb_DragDelta);//绑定拖拽事件
            thumbB.MouseEnter += new MouseEventHandler(thumb_MouseEnter);//绑定鼠标进入该区域的事件
            CanItem.Children.Add(thumbB);//添加控件到画布上
        }

        /// <summary>
        /// 删除选择上的区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public bool DelAreaCompleted()
        {
            if (!isPoly)
            {
                if (curentName != null)
                {
                    //删除当前的矩形和围绕在矩形边上的Thumb控件
                    if (MainMap.FindName(curentName) != null)
                    {
                        MainMap.Children.Remove((Rectangle)(MainMap.FindName(curentName)));
                        //跟着一起删除
                        MainMap.Children.Remove((TextBlock)MainMap.FindName(curentName + "abc"));
                    }
                    else
                    {
                        return false;
                    }
                    if (MainMap.FindName("thumb0") != null) MainMap.Children.Remove((Thumb)(MainMap.FindName("thumb0")));
                    if (MainMap.FindName("thumb1") != null) MainMap.Children.Remove((Thumb)(MainMap.FindName("thumb1")));
                    if (MainMap.FindName("thumb2") != null) MainMap.Children.Remove((Thumb)(MainMap.FindName("thumb2")));
                    if (MainMap.FindName("thumb3") != null) MainMap.Children.Remove((Thumb)(MainMap.FindName("thumb3")));
                    return true;


                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public bool ChangeTextBlock(double FontSize)
        {
            if (!isPoly)
            {
                if (curentName != null)
                {
                    if (MainMap.FindName(curentName) != null)
                    {
                        //改变TextBlock属性
                        ((TextBlock)MainMap.FindName(curentName + "abc")).FontSize = FontSize;
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}
